package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author yali
 * @since 2022-03-13
 * http://localhost:8001/serviceedu/edu-teacher/findAll
 * 用了swagger工具后的网址：http://localhost:8001/swagger-ui.html
 */
@Api(description = "讲师管理")
@CrossOrigin
@RestController
@RequestMapping("/serviceedu/edu-teacher")
public class EduTeacherController {

    //把service注入
    @Autowired
    private EduTeacherService teacherService;

    /**
     * 1 查询讲师表所有数据
     * @return
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("item",list);
    }

    /**
     * 2 逻辑删除讲师
     * @param id
     * @return
     * delete测试：因为是delete提交  因此要用工具进行测试 swagger
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")//id值需要通过路径进行传递
    public R deleteTeacher(@ApiParam(name = "id", value = "讲师id", required = true)@PathVariable String id) {
        boolean removeById = teacherService.removeById(id);
        if (removeById) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    /**
     *
     * @param page 当前页
     * @param limit 每页的记录数
     * @return
     */
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<EduTeacher> pageParam = new Page<>(page, limit);

        //调用方法，把所有数据封装到pageParam中
        teacherService.page(pageParam, null);
        
        long total = pageParam.getTotal();//获取总记录数
        List<EduTeacher> records = pageParam.getRecords();//获取分页后的list集合

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    /**
     * 4.多条件组合查询带分页方法
     * 把条件值封装对象里面，把对象传到接口里面
     * @param current
     * @param limit
     * @param teacherQuery 对象
     * RequstBody：使用json传递数据，把json数据封装到对应对象里面
     * 需要Post提交 required = false：表示参数值可以为空
     * @return
     */
    @ApiOperation(value = "条件查询分页方法")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page
        Page<EduTeacher> pageCondition = new Page<>(current, limit);

        //QueryWrapper,构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        //条件组合查询，动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();//注册的开始时间
        String end = teacherQuery.getEnd();//注册的结束时间

        //判断条件是否为空，如果不为空拼接条件  StringUtil是个工具类
        if (!StringUtils.isEmpty(level)) {
            wrapper.like("name", name);//名字模糊查询
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("level", level);//级别 等于查询
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);//ge大于等于开始时间
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);//le小于等于结束时间
        }

        wrapper.orderByDesc("gmt_create");

        //调用方法，实现分页查询
        teacherService.page(pageCondition, wrapper);

        long total = pageCondition.getTotal();//获取总记录数
        List<EduTeacher> records = pageCondition.getRecords();//获取分页后的list集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }


    /**
     * 添加教师
     * @param eduTeacher
     * @return
     */
    @ApiOperation("添加教师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else
            return R.error();
    }

    /**
     * 根据ID查询教师
     * @param id 通过路径查询
     * @return
     */
    @ApiOperation("根据ID查询教师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher byId = teacherService.getById(id);
        return R.ok().data("teacher", byId);
    }

    /**
     * 讲师修改功能
     * 表单 --对象传递
     * @param eduTeacher
     * @return
     */
    @ApiOperation("修改教师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = teacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        } else
            return R.error();
    }
}

